#import <Cocoa/Cocoa.h>
#include "render.h"
#include "system.h"

int ufoBeginRender(void* param)
{
    NSOpenGLContext *openGLContext = (NSOpenGLContext *)param;
    if (openGLContext) {
        [openGLContext makeCurrentContext];
        CGLLockContext([openGLContext CGLContextObj]);
        return 1;
    }
	return 0;
}

int ufoEndRender(void* param)
{
    NSOpenGLContext *openGLContext = (NSOpenGLContext *)param;
    if (openGLContext) {
        CGLFlushDrawable([openGLContext CGLContextObj]);
        CGLUnlockContext([openGLContext CGLContextObj]);
        return 1;
    }
    return 0;
}

int ufoRenderText(const char *text, ufoRect *rect, const char *font, int fontSize, int bold, ufoColor color, int format, unsigned char *output)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *labelString = [NSString stringWithUTF8String:text];
    
    NSFont *_font = nil;
    if (!bold)
        _font = [NSFont systemFontOfSize:fontSize*ufoGetDPI()];
    else
        _font = [NSFont boldSystemFontOfSize:fontSize*ufoGetDPI()];
    
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:_font, NSFontAttributeName, [NSColor colorWithCalibratedRed:color.red green:color.green blue:color.blue alpha:1.0], NSForegroundColorAttributeName, nil];
    
    NSSize textSize = {rect->right-rect->left, rect->bottom-rect->top};
    
    NSRect textRect = [labelString boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingDisableScreenFontSubstitution  attributes:attributes];
    
    rect->right = (int)(textRect.size.width+0.9999f);
    rect->bottom = (int)(textRect.size.height+0.9999f);
    
    int width = rect->right;
    int height = rect->bottom;
    
    if (format & UFO_TEXT_CALCRECT) {
        return 1;
    }
    
    NSRect offscreenRect = NSMakeRect(0.0, 0.0, rect->right, rect->bottom);
    NSBitmapImageRep* offscreenRep = nil;
    
    offscreenRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
                                                           pixelsWide:rect->right
                                                           pixelsHigh:rect->bottom
                                                        bitsPerSample:8
                                                      samplesPerPixel:4
                                                             hasAlpha:YES
                                                             isPlanar:NO
                                                       colorSpaceName:NSCalibratedRGBColorSpace
                                                         bitmapFormat:0
                                                          bytesPerRow:(4 * rect->right)
                                                         bitsPerPixel:32];
    
    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext:[NSGraphicsContext
                                          graphicsContextWithBitmapImageRep:offscreenRep]];
    
    [labelString drawInRect:offscreenRect withAttributes:attributes];
    
    [NSGraphicsContext restoreGraphicsState];
    
    unsigned char *pBitmapData = [offscreenRep bitmapData];
    pBitmapData += (height-1)*(width*4);
    
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            
            output[4*x+0] = pBitmapData[4*x+0];
            output[4*x+1] = pBitmapData[4*x+1];
            output[4*x+2] = pBitmapData[4*x+2];
            output[4*x+3] = pBitmapData[4*x+3];
        }
        output += width*4;
        pBitmapData -= width*4;
    }
    
    [offscreenRep release];
    //[attributes release];
    [pool release];
    return 1;
}